package com.example.administrator.demo_dialog;



import android.app.Activity;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.drm.ProcessedData;
import android.view.View;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {
    private AlertDialog.Builder builder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        builder=new Builder(this);
    }
    //确认对话框
    public void confirm(View view){

        builder.setTitle("Warning:");
        builder.setMessage("There is a problem in your code on line33,are u sure?");
        builder.setPositiveButton("OK,I got it!", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"好的，我知道了！",Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("Ignore it!", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"别管他",Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }

    //单选对话框
    public void radio(View view){
        builder.setTitle("警告：下列错误的是");
        final String[] items={"A","B","C"};
        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"你的答案是："+items[which],Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消答题",null);
        builder.setPositiveButton("提交",null);
        builder.show();
    }


    //多选对话框
    public void cbox(View view){
        builder.setTitle("下列选项正确的是：(多选)");
        final String[] items={"A","B","C"};
        final boolean[] checked=new boolean[]{false,false,false};
        builder.setMultiChoiceItems(items, checked, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                Toast.makeText(MainActivity.this,"你的答案是："+items[which]+isChecked,Toast.LENGTH_SHORT).show();
                checked[which]=isChecked;
            }
        });
        builder.setNegativeButton("取消答题", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setPositiveButton("提交",null);
        builder.show();
    }

    //进度对话框
    public void progress(View view){
        final ProgressDialog pd=new ProgressDialog(this);
        pd.setTitle("Tips");
        pd.setMessage("The program is loading,Please wait...");
        pd.show();

        new Thread(){
          public void run(){
              try {
                  Thread.sleep(3000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              pd.dismiss();
          }
        }.start();
    }

    //伪进度条对话框
    public void pro(View view){
        final ProgressDialog pd=new ProgressDialog(this);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setMax(10);
        pd.setTitle("Tips");
        pd.setMessage("The program is loading,Please wait...");
        pd.show();
        new Thread(){
            public void run(){
                for (int i=0;i<=10;i++){
                    try {
                        Thread.sleep(500);

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    pd.setProgress(i);
                }
                    pd.dismiss();
            }
        }.start();
    }
}
